import store from '@store/index'
import { fallbackRequestPost } from '@/utils/Request'
import { dbConstants, ResultCode } from '@/constants/constants'
import constants from '@store/user/constants'

/**
 * 请求前缀，后面后台添加网关后需要前缀选择微服务
 */
const URL_PREFIX = 'middle-user/'

/**
 * 账号密码登陆
 * @param {String} username 用户名，目前只支持邮箱和手机
 * @param {String} password 登陆密码
 * @param {String} code 验证码
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} errorCallback 失败回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const loginByPass = (username, password, code, successCallback = null, errorCallback = null, fullCallback = null) => {
  const params = {
    'username': username,
    'password': password,
    'userType': 'USER',
    'type': username.indexOf('@') === -1 ? dbConstants.bindingType.Phone : dbConstants.bindingType.Email,
    'code': code
  }

  return fallbackRequestPost(URL_PREFIX + 'user/login', params, {}, true, data => {
    if (successCallback) {
      // 保存登陆用户信息
      store.commit('user/' + constants.MUTATIONS_SET_LOGIN_USER_INFO, data.object)
      successCallback(data)
    }
  }, errorCallback, fullCallback)
}

/**
 * 获取注册验证码
 * @param {String} username 用户名，目前只能邮箱或者手机号码
 * @param {String} code 图片验证码
 * @param {Function} callback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const registerCode = (username, code, callback = null, fullCallback = null) => {
  const params = {
    'account': username,
    'type': username.indexOf('@') === -1 ? dbConstants.bindingType.Phone : dbConstants.bindingType.Email,
    'code': code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/sendRegisterCode', params, {}, true, callback, null, fullCallback)
}

/**
 * 账号注册
 * @param {String} username 用户名，目前只支持邮箱和手机
 * @param {String} password 设置的密码
 * @param {String} verifyCode 验证码
 * @param {Function} successCallback 成功回调，参数为data
 * @param {Function} fullCallback 肯定触发的回调，先于callback
 */
export const requestRegister = (username, password, verifyCode, successCallback = null, fullCallback = null) => {
  const params = {
    'username': username,
    'nickname': '',
    'password': password,
    'type': username.indexOf('@') === -1 ? dbConstants.bindingType.Phone : dbConstants.bindingType.Email,
    'code': verifyCode
  }
  return fallbackRequestPost(URL_PREFIX + 'user/register', params, {}, true, successCallback, null, fullCallback)
}

// noinspection JSCommentMatchesSignature
/**
 * 通过验证码形式绑定账户
 * @param {String} account 绑定账户
 * @param {Integer} type  绑定账户类型
 * @param {String} code  发送的验证码
 */
export const bindingByCode = (account, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/binding', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 忘记密码，通过发送验证码修改密码
 * @param {String} account 验证账号
 * @param {Integer} type 账号类型
 * @param {String} newPass 新密码
 * @param {String} code  验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const changePassByCode = (account, type, newPass, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    newPass,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/changePassByCode', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 通过原密码修改密码
 * @param {String} oldPass 原密码
 * @param {String} newPass 新密码
 * @param {String} code  验证码，这里是图片验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const changePassByPass = (oldPass, newPass, code, successCallback = null, fullCallback = null) => {
  const params = {
    oldPass,
    newPass,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/changePassByPass', params, {}, true, successCallback, null, fullCallback)
}

const saveLoginInfo = (data) => {
  if (data && data.code === ResultCode.OK) {
    const userInfo = data.object
    store.commit('user/' + constants.MUTATIONS_SET_LOGIN_USER_INFO, userInfo)
    localStorage.setItem('token', userInfo['token'])
  }
}

/**
 * 通过发送随机验证码登陆系统
 * @param {String} username 登陆账号
 * @param {Integer} type 登陆账号类型
 * @param {String} code  验证码，这里是短信发送的验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const loginByCode = (username, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    username,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/loginByCode', params, {}, true, data => {
    saveLoginInfo(data)
    if (successCallback) {
      successCallback(data)
    }
  }, null, fullCallback)
}

/**
 * 通过token登陆系统
 * @param {String} username 登陆账号
 * @param {Integer} type 登陆账号类型
 * @param {String} token token
 * @param {Function} successCallback 成功回调
 * @param {Function} errorCallback 失败回调
 * @param {Function} fullCallback 全回调
 */
export const loginByToken = (username, type, token, successCallback = null, errorCallback = null, fullCallback = null) => {
  const params = {
    username,
    type,
    token
  }
  return fallbackRequestPost(URL_PREFIX + 'user/loginByToken', params, {}, true, data => {
    saveLoginInfo(data)
    if (successCallback) {
      successCallback(data)
    }
  }, errorCallback, fullCallback)
}

/**
 * 退出当前登陆账号
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const logout = (successCallback = null, fullCallback = null) => {
  const params = {
  }
  return fallbackRequestPost(URL_PREFIX + 'user/logout', params, {}, true, (data) => {
    // 保存登陆用户信息
    store.commit('user/' + constants.MUTATIONS_SET_LOGIN_USER_INFO, null)
    localStorage.setItem('token', '')
    successCallback(data)
  }, null, fullCallback)
}

/**
 * 发送用户绑定账户的验证码
 * @param {String} account 绑定账号
 * @param {Integer} type 登陆账号类型
 * @param {String} code  验证码，图片验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const sendBindingCode = (account, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/sendBindingCode', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 发送用户更改密码的验证码
 * @param {String} account 验证账号
 * @param {Integer} type 登陆账号类型
 * @param {String} code  验证码，图片验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const sendChangePassCode = (account, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/sendChangePassCode', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 发送用户登陆的验证码
 * @param {String} account 登陆账号
 * @param {Integer} type 登陆账号类型
 * @param {String} code  验证码，图片验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const sendLoginCode = (account, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/sendLoginCode', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 发送解绑的验证码
 * @param {String} account 解绑账号
 * @param {Integer} type 解绑账号类型
 * @param {String} code  验证码，图片验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const sendUnbindCode = (account, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/sendUnbindCode', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 通过发送的验证码解绑账号
 * @param {String} account 解绑账号
 * @param {Integer} type 解绑账号类型
 * @param {String} code  验证码，这里是短信发送的验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const unbindByCode = (account, type, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/unbindByCode', params, {}, true, successCallback, null, fullCallback)
}

/**
 * 通过密码解绑账号
 * @param {String} account 解绑账号
 * @param {Integer} type 解绑账号类型
 * @param {String} password 密码
 * @param {String} code  验证码，图片验证码
 * @param {Function} successCallback 成功回调
 * @param {Function} fullCallback 全回调
 */
export const unbindByPass = (account, type, password, code, successCallback = null, fullCallback = null) => {
  const params = {
    account,
    type,
    password,
    code
  }
  return fallbackRequestPost(URL_PREFIX + 'user/unbindByPass', params, {}, true, successCallback, null, fullCallback)
}

// noinspection JSUnusedGlobalSymbols
/**
 * userController api结合
 */
export const userController = {
  loginByPass,
  registerCode,
  requestRegister,
  bindingByCode,
  changePassByCode,
  changePassByPass,
  loginByCode,
  loginByToken,
  logout,
  sendBindingCode,
  sendChangePassCode,
  sendLoginCode,
  sendUnbindCode,
  unbindByCode,
  unbindByPass
}

// noinspection JSUnusedGlobalSymbols
export default userController
